package cn.fan.config.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class CommonConfig implements ApplicationContextAware { // 继承ApplicationContextAware接口，bean工厂准备好后会进行通知，执行下方方法

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 获取rabbitTemplate对象
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        // 配置ReturnCallback
        rabbitTemplate.setReturnCallback((message, i, s, s1, s2) -> {
            log.error("消息发送失败，应答码{}，原因{}，交换机{}，路由键{},消息{}",
                    i, s, s1, s2, message.toString());
            // 也可做消息重发，或通知技术人员操作
        });
    }
}
